/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2023-12-03
 * Time: 14:24
 */
public class MySingleList implements IList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //链表的属性  链表的头节点
    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }else{
            ListNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }

    }

    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            throw new IndexException("index不合法的: "+index);
        }
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = searchPrevIndex(index);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode searchPrevIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            } else {
                cur = cur.next;
            }

        }

        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            return;
        }
        if(head.val == key) {
            head = head.next;
        }

        ListNode cur = head;

        while(cur.next != null){
            if(cur.next.val == key) {
                cur.next =cur.next.next;
                return;
            }
            cur = cur.next;
        }

        System.out.println("链表中没有要找的这个数字！");
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }
        while(head.val == key) {
            head = head.next;
        }

        ListNode cur = head;

        while(cur.next != null){
            if(cur.next.val == key) {
                cur.next =cur.next.next;
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int length = 0;
        ListNode cur =head;
        while(cur != null){
            cur= cur.next;
            length++;
        }
        return length;
    }

    @Override
    public void clear() {
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val +" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
